import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/study/train_exam_appro_list_page.dart';
import 'package:flutter_learn_vi/src/page/train/model/train_model.dart';
import 'package:flutter_learn_vi/src/page/train/tain_main_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';

import 'main_exam_page.dart';
import 'main_video_v2_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/12.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MainStudyPage extends StatefulWidget {
  @override
  _MainStudyPageState createState() => _MainStudyPageState();
}

class _MainStudyPageState extends State<MainStudyPage> with TrainModel {
  @override
  void initState() {
    super.initState();
    getMyTrainListNumberFunction();
  }

  @override
  Widget build(BuildContext context) {
    UserBean userBean = UserHelper.getInstance.userBean;
    //对应的审批 默认为0 无审批权限 2为二级审批 1为一级审批
    int isApprove = userBean.isApprove;
    return Scaffold(
      backgroundColor: Colors.white,
      //填充布局
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: SingleChildScrollView(
            padding: EdgeInsets.zero,
            physics: BouncingScrollPhysics(),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(
                  height: 0,
                ),
                //第一部分 第一层 渐变背景
                //buildBackground(),
                //第二部分 第二层 气泡
                //BubbleWidget(),

                buildItemContainer(
                    imagePath: "assets/images/2.0x/study2.png",
                    text: "学习阅读",
                    color: Color(0xff37967f),
                    bottomText: "党的建设、学习视频、安全知识...",
                    click: () {
                      NavigatorUtils.pushPage(context, VideoStudyMainV2Page());
                    }),
                buildTrainFunction(),

                Hero(
                  tag: "pcTest",
                  child: Material(
                    color: Colors.transparent,
                    child: buildItemContainer(
                        imagePath: "assets/images/2.0x/study1.png",
                        text: "考试模式",
                        color: Color(0xff1b83c9),
                        bottomText: "每日一题、快速练习、专项考试...",
                        click: () {
                          NavigatorUtils.openPageByFade(
                              context, MainExamPage());
                        }),
                  ),
                ),

                isApprove == 0
                    ? SizedBox()
                    : buildItemContainer(
                        imagePath: "assets/images/2.0x/study4.png",
                        text: "我的审批",
                        color: Color(0xffFF9644),
                        bottomText: "学员提交的培训考核审批...",
                        click: () {
                          NavigatorUtils.pushPage(
                              context, TrainExamApproListPage());
                        }),
              ],
            ),
          )),
    );
  }

  Widget buildTrainFunction() {
    //userBean.userType == 12 ? "教师中心" :
    UserBean userBean = UserHelper.getInstance.userBean;
    return buildItemContainer(
        imagePath: "assets/images/2.0x/study3.png",
        text: "我的培训",
        bottomText: _trainNumberBean == null
            ? ""
            : "${_trainNumberBean.trainNumber}项培训进行中、${_trainNumberBean.finishNumber}项已完成...",
        color: Color(0xff90b921),
        click: () {
          NavigatorUtils.pushPage(context, TrainMainPage());
        });
  }

  Widget buildItemContainer(
      {String imagePath,
      String text,
      Color color,
      Function click,
      String bottomText}) {
    return InkWell(
      onTap: () {
        if (click != null) {
          click();
        }
      },
      child: Container(
        margin: EdgeInsets.all(20),
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(boxShadow: [
          BoxShadow(
              color: color.withOpacity(0.4),
              spreadRadius: 1,
              blurRadius: 2,
              offset: Offset(1, 1)),
        ], color: color, borderRadius: BorderRadius.all(Radius.circular(10))),
        height: 120,
        child: Column(
          children: [
            Row(
              children: [
                Image.asset(
                  imagePath,
                  width: 38,
                ),
                SizedBox(
                  width: 20,
                ),
                Expanded(
                  child: Text(
                    "$text",
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                ),
                SizedBox(
                  width: 40,
                ),
              ],
            ),
            SizedBox(
              height: 22,
            ),
            Row(
              children: [
                Expanded(
                  child: Text(
                    bottomText,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(fontSize: 12, color: Colors.white),
                  ),
                ),
                Image.asset(
                  "assets/images/2.0x/arror_right.png",
                  width: 14,
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  //第一部分 第一层 渐变背景
  Container buildBackground() {
    return Container(
      decoration: BoxDecoration(
        //线性渐变
        gradient: LinearGradient(
          //渐变角度
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          //渐变颜色组
          colors: [
            Colors.lightBlue.withOpacity(0.3),
            Colors.lightBlueAccent.withOpacity(0.3),
            Colors.blue.withOpacity(0.3),
          ],
        ),
      ),
    );
  }

  TrainNumberBean _trainNumberBean;

  Future<bool> getMyTrainListNumberFunction() async {
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> monthMap = {
      "userType": userBean.userType,
      "department": userBean.department,
    };
    String url = HttpHelper.trainListNumber;
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.getRequest(
      url: url,
      queryParameters: monthMap,
    );

    if (statisRresponseInfo.success) {
      _trainNumberBean = TrainNumberBean.fromJson(statisRresponseInfo.data);
    }

    if (mounted) {
      setState(() {});
    }
    return Future.value(true);
  }
}
